package zcj.stu.controller;

import org.springframework.web.bind.annotation.*;
import zcj.stu.Entity.Goods;
import zcj.stu.service.CommonService;
import zcj.stu.service.GoodsService;
import zcj.stu.utils.Result;

import javax.annotation.Resource;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private CommonService commonService;

    @GetMapping("/getAllShops")
    public Result getAllShops(){
        try {
            return goodsService.getAllShops();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @DeleteMapping("/del/{_id}")
    public Result del(@PathVariable("_id") String _id){
        try {
            boolean isSuccess = goodsService.del(_id);
            if (isSuccess) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @PutMapping("edit")
    public Result edit(@RequestBody Goods goods) {
        try {
            boolean isSuccess = goodsService.edit(goods);
            if (isSuccess) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @GetMapping("/getGoods")
    public Result getGoodsPage(int currentPage, int pageSize,String search) {
        try {
            return commonService.getAccountPage(currentPage, pageSize, Goods.class,search);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @PostMapping("/add")
    public Result add(@RequestBody Goods goods) {
        try {
            boolean isSuccess = goodsService.add(goods);
            if (isSuccess) {
                return Result.ok();
            } else {
                return Result.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
}
